package org.jeecg.modules.fund.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.fund.entity.FundHold;
import org.jeecg.modules.fund.entity.FundLibrary;
import org.jeecg.modules.fund.entity.FundOptional;
import org.jeecg.modules.fund.mapper.FundHoldMapper;
import org.jeecg.modules.fund.service.IFundHoldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "持有管理")
@RequestMapping("/sys/fund/hold")
@Slf4j
public class FundHoldController {
    @Autowired
    private IFundHoldService holdService;
    @Autowired
    private FundHoldMapper holdMapper;

    /**
     * 添加持有
     *
     * @param holds
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<List<FundHold>> add(@RequestBody List<FundHold> holds) {
        return holdService.add(holds);
    }

    /**
     * 删除持有
     * @param hold
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<String> delete(@RequestBody FundHold hold) {
        holdService.removeById(hold.getId());
        return Result.OK("删除成功");
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public Result<FundHold> detail(@RequestParam("optId")String id) {
        FundHold hold = holdService.getById(id);
        return Result.OK(hold);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result<FundOptional> update(@RequestBody FundHold hold) {
        holdService.updateById(hold);
        return Result.ok("更新成功");
    }


    /**
     * 自选分页
     *
     * @param fundLibrary
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<Map<String, Object>>> queryPageList(FundLibrary fundLibrary,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                            HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        fundLibrary.setUserId(sysUser.getId());
        // 分页设置
        Page<FundLibrary> page = new Page<>(pageNo, pageSize);
        // 分页
        IPage<Map<String, Object>> pageList = holdMapper.selectPageVo(page, fundLibrary);
        return Result.OK(pageList);
    }

    /**
     * 持有统计
     *
     * @param groupId
     */
    @RequestMapping(value = "/findDetailCount", method = RequestMethod.GET)
    public Result<Map<String,Object>> findDetailCount(@RequestParam(name = "groupId") String groupId) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return Result.OK(holdMapper.findDetailCount(sysUser.getId(),groupId));
    }

    /**
     * 获取详情（含基金名称等）
     * @param id
     * @return
     */
    @RequestMapping(value = "/detailAll", method = RequestMethod.GET)
    public Result<Map<String,Object>> detailAll(@RequestParam(name = "id") String id) {
        return Result.OK(holdMapper.detail(id));
    }
}
